<template>
	<view class="add">
		<SearchList />
		<!-- 主体部分 -->
		<view class="content" v-if="categoryList[activeIndex]">
			<view class="left">
				<!-- <view class="cate1 active">
					大家电
				</view> -->
				<view class="cate1" :class="{active:activeIndex===index}" v-for="(cate1,index) in categoryList" :key="index" @click="seletCate1(index)">
					{{cate1.cat_name}}
				</view>
			</view>
			<scroll-view class="rigth" scroll-y="true" :scroll-top="scrollTop">
				<image class="nerrhs" src="../../static/images/titleImage.png" mode=""></image>
				<view class="cate2" v-for="(cate2,indexs) in categoryList[activeIndex].children" :key="indexs">
					<view class="title">
						<text>{{cate2.cat_name}}</text>
					</view>
					<!-- 三级分类 -->
					<view class="cate3-wrapper">
						<view class="cate3" v-for="(cate3,i) in cate2.children" :key="i" @click="toSearchList(cate3.cat_name)">
							<image :src="cate3.cat_icon" mode=""></image>
							<view class="">
								{{cate3.cat_name}}
							</view>
						</view>
					</view>

				</view>
			</scroll-view>
		</view>
	</view>

</template>
<script>
	import SearchList from '../../compoents/SearchList.vue'
	export default {
		components: {
			SearchList
		},
		data() {
			return {
				activeIndex: 0,
				categoryList: [],
				scrollTop: 0,
				// old: {
				// 	scrollTop: 0
				// }
			}
		},
		onLoad() {
			this.getCategoryList()
		},
		methods: {
			// 点击跳转商品列表
			toSearchList(catName) {
				uni.navigateTo({
					url: '/pages/search_list/search_list?catName=' + catName
				})
			},
			// 记录滚动的位置
			// scroll: function(e) {
			// 	console.log(e)
			// 	this.old.scrollTop = e.detail.scrollTop
			// },
			// 点击一级标题滚动右侧顶部
			seletCate1(index) {
				this.activeIndex = index
				this.scrollTop = Math.ceil(Math.random() * 100)
				// 记录滚动的位置 然后恢复原来的位置等于0 防抖
				// this.old.scrollTop = e.detail.scrollTop
				this.$nextTick(() => {
					this.scrollTop = 0
				})
			},
			async getCategoryList() {
				this.categoryList = await this.$request({
					url: '/api/public/v1/categories'
				})
			}
		}
	}
</script>

<style scoped lang="less">
	.content {
		display: flex;
		position: absolute;
		top: 100rpx;
		left: 0;
		right: 0;
		bottom: 0;

		.left {
			width: 198rpx;
			overflow: auto;

			.cate1 {
				height: 100rpx;
				line-height: 100rpx;
				text-align: center;
				box-sizing: border-box;
				border-bottom: 1rpx solid #eee;
				background-color: #f4f4f4;

				&.active {
					color: #eb4450;
					background-color: #FFFFFF;
					font-weight: 600;
					position: relative;

					&::before {
						content: '';
						position: absolute;
						width: 8rpx;
						height: 60rpx;
						background-color: #eb4450;
						left: 0;
						top: 20rpx;
					}
				}
			}
		}

		.rigth {
			flex: 1;
			overflow: scroll;
			padding: 20rpx 16rpx 0;

			.nerrhs {
				width: 520rpx;
				height: 180rpx;
			}

			.cate2 {
				margin-bottom: 60rpx;

				.title {
					height: 100rpx;
					display: flex;
					justify-content: center;
					align-items: center;
					color: #e0e0e0;

					text {
						margin: 0 30rpx;
						color: #333;
					}
				}
			}

			.cate3-wrapper {
				display: flex;
				flex-wrap: wrap;
			}

			.cate3 {
				margin-bottom: 20rpx;
				width: 33.33%;
				text-align: center;

				image {
					width: 120rpx;
					height: 120rpx;
				}
			}
		}
	}
</style>
